home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
bplus25a.zip
/
CPLUS.C
< prev
next >
Wrap
Text File
|
1990-12-30
|
40KB
|
1,049 lines
/********************************************************************/
/* */
/* BPLUS file indexing program - Version 2.5 */
/* */
/* A "shareware program" */
/* */
/* */
/* Copyright (C) 1987-1990 by */
/* */
/* Hunter and Associates */
/* 7900 Edgewater Drive */
/* Wilsonville, Oregon 97070 */
/* (503) 694 - 1449 */
/* */
/********************************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <sys\types.h> /* delete this line for Turbo C */
#include <sys\stat.h>
#include <string.h>
#include "bplus.h"
/* macros, constants, data types */
#define NULLREC (-1L) /* special value for RECPOS variable */
#define FREE_BLOCK (-2) /* designates a free block in index file */
/* the address of an entry in a block */
#define ENT_ADR(pb,off) ((ENTRY*)((char*)((pb)->entries) + off))
/* the size of an entry */
#define ENT_SIZE(pe) strlen((pe)->key) + 1 + 2 * sizeof(RECPOS)
/* the cache changed indicator */
#define BUFDIRTY(j) (mci->cache[j].dirty)
/* the index file handle for memblock j */
#define BUFHANDLE(j) (mci->cache[j].handle)
/* memory cache block j */
#define BUFBLOCK(j) (mci->cache[j].mb)
/* number of times cache blk j is referenced */
#define BUFCOUNT(j) (mci->cache[j].count)
/* address of current block for level j */
#define CB(j) (pci->pos[j].cblock)
/* offset of current block for level j */
#define CO(j) (pci->pos[j].coffset)
#define TRUE 1
#define FALSE 0
/* declare some global variables */
IX_DESC *pci; /* pointer to index descriptor */
IX_BUFFER bt_buffer; /* memory cache for index blocks */
IX_BUFFER *mci = &bt_buffer; /* pointer to cache index blocks */
BLOCK *block_ptr; /* pointer to index record block */
BLOCK *spare_block; /* pointer to spare index block */
int cache_ptr = 0; /* index to cache memory pool */
int cache_init = 0; /* 1 when cache is initilized */
int split_size = IXB_SPACE; /* split block when greater than */
int comb_size = (IXB_SPACE/2); /* combine blocks when less than */
/* #define memmove memcpy */ /* Use this macro for MicroSoft C 4.0 */
/* list all function prototypes */
void pascal error(int, long);
void pascal read_if(long, char *, int);
void pascal write_if(int, long, char *, int);
int pascal creat_if(char *);
int pascal open_if(char *);
void pascal reset_buffers(IX_DESC *);
void pascal close_if(int);
void pascal update_block(void);
void pascal init_cache(void);
int pascal find_cache(RECPOS);
int pascal new_cache(void);
void pascal load_cache(RECPOS);
void pascal get_cache(RECPOS);
void pascal retrieve_block(int, RECPOS);
int pascal prev_entry(int);
int pascal next_entry(int);
void pascal copy_entry(ENTRY *, ENTRY *);
int pascal scan_blk(int);
int pascal last_entry(void);
void pascal write_free( RECPOS, BLOCK *);
RECPOS pascal get_free(void);
int pascal find_block(ENTRY *, int *, int *);
void pascal movedown(BLOCK *, int, int);
void pascal moveup(BLOCK *, int, int);
void pascal ins_block(BLOCK *, ENTRY *, int);
void pascal del_block(BLOCK *, int);
void pascal split(int, ENTRY *, ENTRY *);
void pascal ins_level(int, ENTRY *);
int pascal insert_ix(ENTRY *, IX_DESC *);
int pascal find_ix(ENTRY *, IX_DESC *, int);
int pascal combineblk(RECPOS, int);
void pascal replace_entry(ENTRY *);
/* file I/O for B-PLUS module */
void pascal error(j, l) /* print file error messages */
int j; /* error number */
long l; /* current file position */
{
static char *msg[3] = {"ERROR - CANNOT OPEN/CLOSE FILE",
"ERROR WHILE READING FILE",
"ERROR WHILE WRITING FILE"};
printf("\n %s - Record Number %ld\n", msg[j], l);
exit(1); /* delete this line to not halt program */
/* and call your error handlng routine */
} /* error */
void pascal read_if(start, buf, nwrt) /* read pci index file */
long start; /* file read position */
char *buf; /* data holding buffer */
int nwrt; /* number bytes to read */
{
long err;
/* seek to read position in current index file */
err = start - lseek(pci->ixfile, start, SEEK_SET);
/* if no error read an index file block */
if (err == 0) err = nwrt - read(pci->ixfile, buf, nwrt);
/* call error routine if number bytes read != nwrt */
if (err != 0) error(1, start);
} /* read_if */
void pascal write_if(handle, start, buf, nwrt) /* write index record */
int handle; /* write to this file handle */
long start; /* write to this position in file */
char *buf; /* write data from this buffer */
int nwrt; /* write this many bytes of data */
{
long err;
/* seek to file write position */
err = start - lseek(handle, start, SEEK_SET);
/* if no error write index block block */
if (err == 0) err = nwrt - write(handle, buf, nwrt);
/* call error routine if number bytes written != nwrt */
if (err != 0) error(2, start);
} /* write_if */
int pascal creat_if(fn) /* make a new index file */
char *fn; /* name and path of file */
{
int ret;
ret = open(fn,O_RDWR|O_CREAT|O_TRUNC|O_BINARY,S_IWRITE);
if (ret < 0) error(0,0L); /* there was an error if ret < 0 */
return (ret);
} /* creat_if */
int pascal open_if(fn) /* open an existing index file */
char *fn; /* path and name of index file */
{
int ret;
ret = open(fn,O_RDWR|O_BINARY);
if (ret < 1) error(0,0L); /* there was an error is ret < 1 */
return (ret);
} /* open_if */
void pascal close_if(handle) /* close an open index file */
int handle; /* with this file handle */
{
if(close(handle) < 0) error(2,0L);
} /* close_if */
int cdecl open_index(name, pix, dup) /* open and initilize index file */
char *name; /* path and name of index file */
IX_DESC *pix; /* pointer to index descriptor */
int dup; /* allow duplicate keys if != 0 */
{
pci = pix; /* pci is global index descriptor */
pci->ixfile = open_if(name); /* file handle */
pci->duplicate = dup; /* set duplicate keys flag */
pci->root_dirty = FALSE;
/* read root descriptor for index */
read_if(0L,(char *)&(pix->root), (sizeof(BLOCK) + sizeof(IX_DISK)));
if (!cache_init) /* if cache not initilized */
{
init_cache(); /* initilize cache memory */
cache_init = 1; /* but only once */
}
return ( IX_OK );
} /* open_index */
void cdecl